<?php

class MysqlImportAction extends ShellAction {

    public function __construct($actionName, $cmd) {
   		parent::__construct($actionName, $cmd);       
    }

    public function execute(ezcWorkflowExecution $execution) {
    	$source	= CJSON::decode($execution->getVariable('source'));
		$dest	= CJSON::decode($execution->getVariable('dest'));
		
    	$runtime	= getenv('RUNTIME');
    	$select		= $source['filter']['select'];    	
    	$tableName	= $dest['storage'];
    	$system		= ResourceService::getInstance()->getStorageSystem($dest['system']);
    	
    	$destFile = "{$runtime}/output/{$tableName}";
    	$this->cmd = $this->concreteParams($this->cmd, array(
    		':hostname'	=>	$system['host_name'],
    		':user'		=>	$system['user_name'],
    		':password'	=>	$system['password'],
    		':db'		=>	$system['namespace'],
    		':destFile'	=>	$destFile,
    		':tableName'=>	$tableName,
    		':columnDefinition'	=>	$this->generateColumns($select),
    		)
    	);
 		return parent::execute($execution);
    }
    
	private function generateColumns($select) {    	
    	$columnDefine = '';
    	foreach ($select as $columnName => $columnType) {
    		$columnDefine .= "$columnName $columnType,";
    	}
    	
    	return substr($columnDefine, 0, strlen($columnDefine) - 1);
    }
}
